package com.example.demo;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;

import com.myclover.db.bean.States;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class StateAdapter extends BaseAdapter
{
	private LayoutInflater mInflater;
	private List<States> listItem;
	private int selectedRow = 0;

	public StateAdapter(Context context, List<States> listItem)
	{
		this.mInflater = LayoutInflater.from(context);
		this.listItem = listItem;
	}

	public void setSource(List<States> listItem)
	{
		this.listItem = listItem;
	}

	public int getCount()
	{
		return listItem.size();
	}

	public Object getItem(int position)
	{
		return listItem.get(position);
	}

	public long getItemId(int position)
	{
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parents)
	{
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null)
		{
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.states_items, null);
			holder.tv_state_id = (TextView) convertView.findViewById(R.id.tv_state_id);
			holder.tv_state_name = (TextView) convertView.findViewById(R.id.tv_state_name);
			convertView.setTag(holder);
		} else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tv_state_id.setTextSize(16);
		holder.tv_state_name.setTextSize(16);

		holder.tv_state_id.setText(String.valueOf(listItem.get(position).getState_id()));
		holder.tv_state_name.setText(String.valueOf(listItem.get(position).getState_name()));

		if (position == selectedRow)
		{
			holder.tv_state_id.setBackgroundColor(Color.rgb(255, 219, 16));
			holder.tv_state_name.setBackgroundColor(Color.rgb(255, 219, 16));

		} else
		{
			holder.tv_state_id.setBackgroundColor(Color.rgb(204, 204, 204));
			holder.tv_state_name.setBackgroundColor(Color.rgb(204, 204, 204));

		}
		return convertView;
	}

	public final class ViewHolder
	{
		public TextView tv_state_id;
		public TextView tv_state_name;

	}

	public void ScrollToRow(int row)
	{
		selectedRow = row;
		this.notifyDataSetChanged();
	}

	public int GetSelectedIndex()
	{
		return selectedRow;
	}
}